package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.BehaviorService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.common.constants.HotArticleConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.dtos.ReadBehaviorDto;
import com.heima.model.behavior.dtos.UnLikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.UpdateArticleMess;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.ApUserThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
public class BehaviorServiceImpl implements BehaviorService {

    @Autowired
    private CacheService cacheService;

    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    /**
     * 用户行为点赞
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult likeBehavior(LikesBehaviorDto dto) {
        // 参数校验
        if (dto == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 判断当前用户是否是登录状态
        ApUser user = ApUserThreadLocalUtil.getUser();
        if (user == null || user.getId() == null || user.getId() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        UpdateArticleMess mess = new UpdateArticleMess();
        mess.setArticleId(dto.getArticleId());
        mess.setType(UpdateArticleMess.UpdateArticleType.LIKES);

        // 设置redis的key
        String key = BehaviorConstants.LIKE_BEHAVIOR + dto.getArticleId();

        // 判断用户是要点赞还是取消赞
        if (dto.getOperation() == 0) {
            // 点赞, 判断用户是否已经点过赞了
            // 根据key从redis中查询数据
            String value = (String) cacheService.hGet(key, user.getId().toString());
            if (value != null) {
                // 点过赞了
                return ResponseResult.errorResult(201, "您已经点过赞了,不能重复点赞!");
            }
            // 将点赞信息添加到redis中
            cacheService.hPut(key, user.getId().toString(), JSON.toJSONString(dto));
            mess.setAdd(1);
        } else {
            // 取消点赞,将数据从Redis中删除
            cacheService.hDelete(key, user.getId().toString());
            mess.setAdd(-1);
        }

        // 发送消息
        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC, JSON.toJSONString(mess));

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 文章查看次数统计
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult readBehavior(ReadBehaviorDto dto) {
        // 参数校验
        if (dto == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 设置redis的key
        String key = BehaviorConstants.READ_BEHAVIOR + dto.getArticleId();
        // 获取到当前登录用户
        ApUser user = ApUserThreadLocalUtil.getUser();

        // 判断用户是否是第一次查看该文章
        Integer count = dto.getCount();
        if (count == null) {
            // 第一次查看
            count = 1;
            // 存到redis中   key    当前登录用户id     查看次数
        } else {
            // 不是第一次查看
            count += 1;
            // 存到redis中
        }
        cacheService.hPut(key, user.getId().toString(), count.toString());

        UpdateArticleMess mess = new UpdateArticleMess();
        mess.setArticleId(dto.getArticleId());
        mess.setType(UpdateArticleMess.UpdateArticleType.VIEWS);
        mess.setAdd(1);

        //发送消息
        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC, JSON.toJSONString(mess));

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 不喜欢或者取消不喜欢
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult UnlikesBehavior(UnLikesBehaviorDto dto) {
        // 参数校验
        if (dto == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 判断是否是登录状态
        ApUser user = ApUserThreadLocalUtil.getUser();
        if (user == null || user.getId() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        // 设置key
        String key = BehaviorConstants.UN_LIKE_BEHAVIOR + dto.getArticleId();

        // 判断用户当前操作是设置为不喜欢还是取消不喜欢
        if (dto.getType() == 0) {
            // 设置不喜欢, 将结果添加到redis中
            cacheService.hPut(key, user.getId().toString(), JSON.toJSONString(dto));
        } else {
            // 删除数据
            cacheService.hDelete(key, user.getId().toString());
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
